package com.zkthink.ceres.pay.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableAutoConfiguration
@Configuration
public class AliPayConfiguration {
    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    @Value("${ceres.ali.pay.app_id}")
    public String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    @Value("${ceres.ali.pay.merchant_private_key}")
    public String merchant_private_key;

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    @Value("${ceres.ali.pay.alipay_public_key}")
    public String alipay_public_key;

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    @Value("${ceres.ali.pay.notify_url}")
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    @Value("${ceres.ali.pay.return_url}")
    public String return_url;

    // 签名方式
    @Value("${ceres.ali.pay.sign_type}")
    public String sign_type;

    // 字符编码格式
    @Value("${ceres.ali.pay.charset}")
    public String charset;

    // 支付宝网关
    @Value("${ceres.ali.pay.gatewayUrl}")
    public String gatewayUrl;

    @Bean
    public AlipayClient alipayClient() {
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);
        return alipayClient;
    }

    @Bean
    public AlipayTradePagePayRequest alipayTradePagePayRequest() {
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        return alipayRequest;
    }

    @Bean
    public AlipayTradeAppPayRequest alipayTradeAppPayRequest() {
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        return alipayRequest;
    }

    @Bean
    public AlipayTradeRefundRequest alipayTradeRefundRequest() {
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        return alipayRequest;
    }
    @Bean
    public AlipayTradeWapPayRequest alipayTradeWapPayRequest(){
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        return alipayRequest;
    }

}
